//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package net.mingsoft.basic.aop;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Filter;
import cn.hutool.core.lang.mutable.MutablePair;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import net.mingsoft.base.entity.BaseEntity;
import net.mingsoft.base.entity.ResultData;
import net.mingsoft.basic.annotation.LogAnn;
import net.mingsoft.basic.biz.ILogBiz;
import net.mingsoft.basic.constant.e.BusinessTypeEnum;
import net.mingsoft.basic.entity.LogEntity;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.basic.util.SpringUtil;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;

@Aspect
public abstract class BaseLogAop extends BaseAop {
    private static final String SUCCESS = "success";
    private static final String ERROR = "error";
    @Autowired
    private ILogBiz logBiz;
    private static final Logger LOG = LoggerFactory.getLogger(SystemLogAop.class);

    public BaseLogAop() {
    }

    public abstract String getUserName();

    public abstract boolean isCut(LogAnn log);

    @Pointcut("@annotation(net.mingsoft.basic.annotation.LogAnn)")
    public void logPointCut() {
    }

    @AfterReturning(
            pointcut = "logPointCut()",
            returning = "result"
    )
    public void doAfterReturning(JoinPoint joinPoint, Object result) {
        this.handleLog(joinPoint, (Exception)null, result);
    }

    @AfterThrowing(
            value = "logPointCut()",
            throwing = "e"
    )
    public void doAfterThrowing(JoinPoint joinPoint, Exception e) {
        this.handleLog(joinPoint, e, (Object)null);
    }

    protected void handleLog(final JoinPoint joinPoint, final Exception e, Object result) {
        try {
            LogAnn controllerLog = (LogAnn)this.getAnnotation(joinPoint, LogAnn.class);
            if (controllerLog == null) {
                return;
            }

            if (!this.isCut(controllerLog)) {
                return;
            }

            LogEntity logEntity = new LogEntity();
            if (controllerLog.saveId()) {
                BaseEntity baseEntity = (BaseEntity)this.getType(joinPoint, BaseEntity.class, true);
                if (baseEntity != null) {
                    logEntity.setBusinessId(baseEntity.getId());
                } else {
                    ArrayList baseEntities = (ArrayList)this.getType(joinPoint, ArrayList.class);
                    if (CollectionUtil.isNotEmpty(baseEntities)) {
                        List<String> ids = (List)baseEntities.stream().map((entity) -> {
                            return entity instanceof BaseEntity ? ((BaseEntity)entity).getId() : null;
                        }).collect(Collectors.toList());
                        ids.remove((Object)null);
                        logEntity.setBusinessId(StrUtil.join(",", ids));
                    }
                }
            }

            logEntity.setLogUser(this.getUserName());
            logEntity.setLogStatus("success");
            String ip = BasicUtil.getIp();
            logEntity.setLogIp(ip);
            logEntity.setLogResult(JSONUtil.parseObj(result).toJSONString(0, new Filter<MutablePair<Object, Object>>() {
                public boolean accept(MutablePair<Object, Object> objectObjectMutablePair) {
                    return !objectObjectMutablePair.getKey().toString().equalsIgnoreCase("id");
                }
            }));
            logEntity.setLogUrl(SpringUtil.getRequest().getRequestURI());
            if (e != null) {
                logEntity.setLogStatus("error");
                logEntity.setLogErrorMsg(StringUtils.substring(e.getMessage(), 0, 4000));
            }

            if (BusinessTypeEnum.LOGIN.getLabel().equalsIgnoreCase(controllerLog.title())) {
                ResultData resultData = (ResultData)result;
                if (!BooleanUtil.toBoolean(resultData.get("result").toString())) {
                    logEntity.setLogStatus("error");
                    logEntity.setLogErrorMsg(String.valueOf(resultData.get("msg")));
                }
            }

            String className = joinPoint.getTarget().getClass().getName();
            String methodName = joinPoint.getSignature().getName();
            logEntity.setLogMethod(className + "." + methodName + "()");
            logEntity.setLogRequestMethod(SpringUtil.getRequest().getMethod());
            logEntity.setLogBusinessType(controllerLog.businessType().name().toLowerCase());
            logEntity.setLogTitle(controllerLog.title());
            logEntity.setLogUserType(controllerLog.operatorType().name().toLowerCase());
            if (controllerLog.saveRequestData()) {
                boolean isJson = StringUtils.isNotBlank(SpringUtil.getRequest().getContentType()) && MediaType.valueOf(SpringUtil.getRequest().getContentType()).includes(MediaType.APPLICATION_JSON);
                String jsonString;
                if (isJson) {
                    Object jsonParam = this.getJsonParam(joinPoint);
                    if (ObjectUtil.isNotNull(jsonParam)) {
                        jsonString = JSONUtil.toJsonPrettyStr(jsonParam);
                        logEntity.setLogParam(jsonString);
                    }
                } else {
                    Map<String, String[]> map = SpringUtil.getRequest().getParameterMap();
                    jsonString = JSONUtil.toJsonPrettyStr(map);
                    logEntity.setLogParam(jsonString);
                }
            }

            logEntity.setCreateDate(new Date());
            this.logBiz.saveData(logEntity);
        } catch (Exception var12) {
            LOG.error("日志记录错误:{}", var12.getMessage());
            var12.printStackTrace();
        }

    }
}
